//
//  MyTextField.m
//  makepack_tools
//
//  Created by tangs on 2018/8/22.
//  Copyright © 2018年 tangs. All rights reserved.
//

#import "MyTextField.h"

@implementation MyTextField

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}

- (void)update {
    if (self.isModify) {
        self.textColor = NSColor.redColor;
    } else {
        self.textColor = NSColor.blackColor;
    }
}

- (void)saveSucc:(NSNotification *)notification {
    self.isModify = false;
    [self update];
}

- (void)onSwitch:(id)sender {
    if (self.data != nil) {
        if (self.index < [self.data count] &&
            [self.stringValue compare:self.data[self.index]] != NSOrderedSame) {
            self.isModify = true;
            [self update];
            [self.data replaceObjectAtIndex:self.index withObject:self.stringValue];
            NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:self.data, @"data", nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"update_cell"
                                                                object:self
                                                              userInfo:dic];
        }
    }
}

- (void)bindData:(NSMutableArray *)data withIndex:(int)index {
    self.data = data;
    self.index = index;
    [self setTarget:self];
    [self setAction:@selector(onSwitch:)];
    [self update];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(saveSucc:)
                                                 name:@"save_succ"
                                               object:nil];
}

@end
